package com.android.loveseason;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SelectPlayerActivity extends Activity implements OnItemClickListener{
	
	private static final String TAG = "SelectPlayerActivity";
	SimpleAdapter ListItemAdapter;
	ArrayList<HashMap<String, Object>> PlayerItemList;
	ListView m_Listview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.selectplayerlistlayout);
		findId();
		
		PlayerItemList  = new ArrayList<HashMap<String,Object>>();
		ContentResolver Resolver = this.getContentResolver();
		
		Cursor mCursor = Resolver.query(LoveSeanProvider.PLAYERINFO_URI,new String[]{"user_id","user_name","user_headid"},null, null,null);
		//ListItemAdapter = new SimpleAdapter(null, headimageList, 0, null, null);
		if(null != mCursor)
		{
			Log.i(TAG,"Load the data");
			//mCursor.moveToFirst();
			while(mCursor.moveToNext()){
				HashMap<String,Object> map = new HashMap<String,Object>();
				map.put("user_id",mCursor.getInt(mCursor.getColumnIndex("user_id")));
				map.put("user_headid", mCursor.getInt(mCursor.getColumnIndex("user_headid")));
				map.put("user_name", mCursor.getString(mCursor.getColumnIndex("user_name")));
				PlayerItemList.add(map);
			}  
			ListItemAdapter = new SimpleAdapter(this,
												PlayerItemList, 
												R.layout.playerselectlistitemlayout,
												new String[]{"user_name","user_headid"},
												new int[]{R.id.PlayerListitem_id,R.id.PlayerListitemImg_id});
			m_Listview.setAdapter(ListItemAdapter);
			m_Listview.setOnItemClickListener(this);
		}
		
		
	}
	private void findId(){
		m_Listview = (ListView)findViewById(R.id.SelectPlayerList_id);
	}
	@Override
	public void onItemClick(AdapterView<?> list, View v, int arg2, long nindex) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		map = PlayerItemList.get((int)nindex);
		int player_id = (Integer) map.get("user_id");
		Intent myIntent = new Intent();
		myIntent.putExtra("user_id",player_id);
		myIntent.setClass(SelectPlayerActivity.this,houseActivity.class);
		startActivity(myIntent);
		finish();
	}
	
}
